package com.cazor.common.utils;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cazor.common.constant.HttpStatus;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Map;

public class JSONResultUtil {

    public static JSONObject result(int code, String msg){
        JSONObject result = new JSONObject(2);
        result.put("code", code);
        result.put("msg", StringUtils.defaultString(msg, ""));
        return result;
    }

    public static JSONObject result(int code, String msg, Map<String, Object> params){
        JSONObject result = new JSONObject(2);
        result.put("code", code);
        result.put("msg", StringUtils.defaultString(msg, ""));
        if (params!=null && params.size()>0){
            result.putAll(params);
        }
        return result;
    }

    public static JSONObject result(int code, String msg, Object obj){
        JSONObject result = new JSONObject(2);
        result.put("code", code);
        result.put("msg", StringUtils.defaultString(msg, ""));
        if (obj!=null){
            result.put("data", obj);
        }
        return result;
    }

    public static JSONObject success(){
        return result(HttpStatus.SUCCESS, "");
    }

    public static JSONObject success(Object obj){
        return result(HttpStatus.SUCCESS, "", obj);
    }

    public static JSONObject success(String msg, Object obj){
        return result(HttpStatus.SUCCESS, msg, obj);
    }

    public static JSONObject warn() {
        return result(HttpStatus.WARN, "");
    };

    public static JSONObject warn(String msg) {
        return result(HttpStatus.WARN, msg);
    }

    public static JSONObject error(){
        return result(HttpStatus.ERROR, "");
    }

    public static JSONObject error(String msg) {
        return result(HttpStatus.ERROR, msg);
    }
}
